package com.kyzh.core.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.beans.Game
import com.gushenge.core.beans.Sort
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.utils.dip
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import org.jetbrains.anko.toast

class RankBottomAdapter(val context: Context, val titles: MutableList<Sort>, val t: Int) : PagerAdapter() {
    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return titles.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = View.inflate(context, R.layout.recyclerview, null)

        val root = view.findViewById<SmartRefreshLayout>(R.id.root)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
        val bean = mutableListOf<Game>()
        recyclerView.layoutManager = object : LinearLayoutManager(context) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }
        }
        val adapter = RankAdapter(R.layout.rank_item, bean)
        recyclerView.adapter = adapter
        var p1 = 1
        var max1 = 1
        root.setEnableAutoLoadMore(false)
        root.setEnableRefresh(true)
        root.autoRefresh()
        root.setOnLoadMoreListener {
            if (p1 > max1) {
                context.toast(context.getString(R.string.noHaveMore))
                root.finishLoadMore()
            } else {
                GameImpl.rank(t, titles[position].id, p1, object : ResultListener {
                    override fun success(beans: Any, p: Int, max: Int) {
                        beans as ArrayList<Game>
                        val gameArray = arrayListOf<Game>()
                        gameArray.addAll(beans)

                        bean.addAll(gameArray)
                        adapter.notifyDataSetChanged()
                        p1 = p
                        max1 = max
                        root.finishLoadMore()
                    }

                    override fun error(error: String) {
                        context.toast(context.getString(R.string.noHaveMore))
                        root.finishLoadMore()
                    }
                })
            }
        }
        root.setOnRefreshListener {
            GameImpl.rank(t, titles[position].id, 1, object : ResultListener {
                override fun success(beans: Any, p: Int, max: Int) {
                    bean.clear()
                    bean.addAll(beans as ArrayList<Game>)
                    adapter.notifyDataSetChanged()
                    p1 = p
                    max1 = max
                    root.finishRefresh()
                }

                override fun error(error: String) {
                    adapter.setEmptyView(R.layout.empty)
                    root.finishRefresh()
                }
            })

        }
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    fun getTabView(position: Int): View {
        val view = LayoutInflater.from(context).inflate(R.layout.rank_tab_item, null)
        val tv = view.findViewById<ArcButton>(R.id.text)
        tv.text = titles[position].name
        tv.setRadius(context.dip(6).toFloat())
        return view
    }

}